package com.example.service;

import cn.hutool.core.date.DateUtil;
import com.example.entity.Account;
import com.example.entity.Admin;
import com.example.entity.Shangpinxiangqing;
import com.example.mapper.ShangpinxiangqingMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShangpinxiangqingService {

    @Resource
    ShangpinxiangqingMapper shangpinxiangqingMapper;

    public void add(Shangpinxiangqing shangpinxiangqing) {
        Account accountUser = TokenUtils.getCurrentUser();
        if ( "ADMIN".equals(accountUser.getRole())){
            shangpinxiangqing.setAdminId(accountUser.getId());
        }
        shangpinxiangqing.setTime(DateUtil.now());
        shangpinxiangqingMapper.insert(shangpinxiangqing);
    }

    public void delete(Integer id) {
        shangpinxiangqingMapper.deleteById(id);
    }

    public void update(Shangpinxiangqing shangpinxiangqing) {
        shangpinxiangqingMapper.updateById(shangpinxiangqing);
    }

    public List<Shangpinxiangqing> selectAll(Shangpinxiangqing shangpinxiangqing) {
        return shangpinxiangqingMapper.selectAll(shangpinxiangqing);
    }

    public PageInfo<Shangpinxiangqing> selectPage(Integer pageNum, Integer pageSize, Shangpinxiangqing shangpinxiangqing) {
        // 开启分页查询
        PageHelper.startPage(pageNum, pageSize);
        List<Shangpinxiangqing> list = shangpinxiangqingMapper.selectAll(shangpinxiangqing);
        return PageInfo.of(list);
    }



}
